// pages/cultureCoupons/powerCoupons/helpPage/index.js
const common = require("../../../public.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showSuccessView: false,
    couponNo: '',
    helpUserInfo: {},
    loadingData: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const couponNo = options.couponNo
    if (!couponNo) {
      common.showToast('参数错误')
      return
    }
    this.data.couponNo = couponNo
    let that = this
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
        })
      },
      fail: function (res) {
        common.loginFunc().then(function (loginSucessed) {
          if (loginSucessed) {
            wx.getStorage({
              key: 'user',
              success: function (res) {
                that.setData({
                  userInfo: res.data.user,
                  token: res.data.token,
                })
              }
            })
          }
        })
      }
    })
    this.getHelpUserInfo()
  },
  getHelpUserInfo() {
    this.setData({
      loadingData: true
    })
    const param = {
      couponNo: this.data.couponNo
    }
    const that = this
    common.post('/cityDisCoupon/getHelperUserInfo', param, function (res) {
      if (res.status === 200) {
        that.setData({
          loadingData: false,
          helpUserInfo: res.data
        })
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  // 助力按钮事件
  helpAction() {
    if (this.data.userInfo === null) {
      common.showToast('获取用户信息失败')
    }
    const param = {
      userId: this.data.userInfo.id,
      couponNo: this.data.couponNo
    }
    const that = this
    common.post('/cityDisCoupon/saveCityDiscountCouponHelper', param, function (res) {
      if (res.status === 200) {
        that.showSuccessView()
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  // 关闭助力成功视图
  closeSuccessView() {
    this.setData({
      showSuccessView: false
    })
  },
  showSuccessView() {
    this.setData({
      showSuccessView: true
    })
  },
  // 跳转到领券页面
  gotoReceive() {
    wx.redirectTo({
      url: '/pages/cultureCoupons/index',
    })
  }
})